/**
  ******************************************************************************
  * @file    demo_api.c 
  * @author  Aizbot
  * @brief   xxx.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 AizBot.
  * All rights reserved.</center></h2>
  *
  * http://www.aizbot.com
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#define DEMO_API_C
#include "demo_api.h"

/* Private typedef -----------------------------------------------------------*/
#if 0
static Zqlink_Msg_t feedback = {
	.ver = ZQLINK_VERSION_A1,
	.data = demo_buf_feedback,
	.tail = ZQLINK_TAIL_NOTUSE
};
#endif

/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void Demo_Cmd_Test(Zqlink_Msg_t *msg, uint8_t cmd);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
void Demo_Api_Init(void)
{
	Zqfifo_Init(&demo_fifo_in,  demo_buf_in,  DEMO_SIZE_PACKET);
	Zqfifo_Init(&demo_fifo_out, demo_buf_out, DEMO_SIZE_PACKET);

	demo_api_handle.dev 	 = DEMO_API_UART;
	demo_api_handle.msg.ver  = ZQLINK_VERSION_A1;
	demo_api_handle.msg.tail = ZQLINK_TAIL_NOTUSE;
	demo_api_handle.msg.data = demo_buf_payload;
	demo_api_handle.Api_RecSuccess = Demo_Api_RecSuccess;
	demo_api_handle.Api_RecError   = Demo_Api_RecError;
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
void Demo_Api_Task(void)
{
	Zqlink_Unpack(&demo_fifo_in, &demo_api_handle);
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
void Demo_Api_SendData(Zqlink_Msg_t *msg, Zqfifo_t *fifo, Zqlink_t *handle)
{
	Zqlink_Packing(msg, fifo);

	if(handle->dev == DEMO_API_UART)
	{
		/* uint8_t data = 0; */
		while(Zqfifo_Count(fifo))
		{
			/* data = Zqfifo_Read(fifo); */
			/* Add your code here , use hardware interface to send data */
			/* HAL_UART_Transmit(&UartHandle, &data, 1, 1000); */
		}
	}
	else if(handle->dev == DEMO_API_BLE)
	{
	}
	else if(handle->dev == DEMO_API_WIFI)
	{
	}
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
void Demo_Api_RecSuccess(Zqlink_Msg_t *msg, uint8_t device)
{
	if(device == DEMO_API_UART)
	{
		if(msg->msgid.maincmd == DEMO_CMD_TEST)
		{
			Demo_Cmd_Test(msg, msg->msgid.cmd);
		}
	}
	else if(device == DEMO_API_BLE)
	{
	}
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
void Demo_Api_RecError(Zqlink_Msg_t *msg, uint8_t device)
{
	AMX_Printf("!!! check error !!!\r\n");
}

// For test
/**
	void __make_msg_fb(Zqlink_Msg_t *fb, Zqlink_Msg_t *msg, u8 ack_flag)
	{
		fb->length = xx;
		fb->seq = xx;
		...
		fb->data[...] = xx;
	}
*/

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
static void fb_test_msg_sub1(Zqlink_Msg_t *msg)
{
	/**you can make the message by yourself:
	
		__make_msg_fb(&feedback, msg, flag);
		Demo_Api_SendData(feedback, &xxx_fifo_out, &xxx_api_handle);
	*/

	// feedback what receive
	Demo_Api_SendData(msg, &demo_fifo_out, &demo_api_handle);
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
static void fb_test_msg_sub2(Zqlink_Msg_t *msg)
{
	/**you can make the message by yourself:
	
		__make_msg_fb(&feedback, msg, flag);
		Demo_Api_SendData(feedback, &xxx_fifo_out, &xxx_api_handle);
	*/

	// feedback what receive
	Demo_Api_SendData(msg, &demo_fifo_out, &demo_api_handle);
}

/**
  * @brief  xxx.
  * @param  None
  * @retval None
  */
static void Demo_Cmd_Test(Zqlink_Msg_t *msg, uint8_t cmd)
{
	switch(cmd)
	{
	case DEMO_CMD_TEST_SUB1:
		fb_test_msg_sub1(msg);
		break;

	case DEMO_CMD_TEST_SUB2:
		fb_test_msg_sub2(msg);
		break;

	default:
		break;
	}
}

/************************ (C) COPYRIGHT Aizbot *****END OF FILE***************/
